import json

from file_define import *
from data_define import Record
from pyecharts.charts import Bar
from pyecharts.options import *
from pyecharts.globals import ThemeType
from pymysql import Connection

text_file_reader = TextFileReader("D:\\file\\2011年1月销售数据.txt")
json_file_reader = JsonFileReader("D:\\file\\2011年2月销售数据JSON.txt")

jan_data: list[Record] = text_file_reader.read_data()
feb_data: list[Record] = json_file_reader.read_data()
# 将两个列表合并
all_data: list[Record] = jan_data + feb_data
# 构建MySQL连接对象
conn = Connection(
    host="localhost",
    port=3306,
    user="root",
    password="cao20001009",
    autocommit=True
)
cursor = conn.cursor()
conn.select_db("py_mysql")
# for record in all_data:
#     sql = (f"insert into orders(order_date,order_id,money,province) values('{record.date}',"
#            f"'{record.order_id}',{record.money},'{record.province}')")
#     # print(sql)
#     cursor.execute(sql)
cursor.execute("select date_format(order_date,'%Y-%m-%d'),order_id,money,province from orders")
result = cursor.fetchall()

f = open("D:\\file\\每日销售额.txt", "w", encoding="utf-8")

# 创建一个空字典

for res in result:
    data_dict = {"date": res[0], "order_id": res[1], "money": res[2], "province": res[3]}
    data_dict_json = json.dumps(data_dict, ensure_ascii=False)

    f.write(data_dict_json)
    f.write("\n")

# 关闭文件
f.close()

# 关闭连接
conn.close()
